package com.zsh.ssmspringboot1.controller;

import com.zsh.ssmspringboot1.model.Category;
import com.zsh.ssmspringboot1.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cate")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @RequestMapping(value = "/hello")
    public String hello(Model model){
        List<Category> list=categoryService.selectAll();
        model.addAttribute("list",list);
        return "/behind/category";
    }
    @RequestMapping(value = "/add")
    @ResponseBody
    public String add(String categoryId,String name){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("categoryId",categoryId);
        map.put("name",name);
        categoryService.add(map);
        return "success";
    }
    @RequestMapping(value = "/delete")
    public String delete(@RequestParam(value = "id",required = true) Long id){
        categoryService.delete(id);
        return "redirect:/cate/hello";
    }
    @RequestMapping(value = "/look")
    public String look(@RequestParam(value = "id",required = true) Long id,Model model){
        Category category=categoryService.select(id);
        model.addAttribute("category",category);
        return "/behind/update";
    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(Category category){
        categoryService.update(category);
        return "success";
    }
}
